package com.atguigu.tingshu.account.config;

import jakarta.annotation.PostConstruct;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/***
 * 账户充值消息队列可靠性投递配置
 */
@Component
@Log4j2
public class AccountRechargeReturnCallBackConfig implements RabbitTemplate.ReturnsCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 可靠性投递配置初始化:仅触发一次
     */
    @PostConstruct
    public void init(){
        rabbitTemplate.setReturnsCallback(this::returnedMessage);
    }

    /**
     * 可靠性投递回调函数配置 -- 消费失败记录日志
     * @param returnedMessage
     */
    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {
        log.error("消息消费失败, 消息内容: " + new String(returnedMessage.getMessage().getBody()) +
                ", 交换机" + returnedMessage.getExchange() +
                ", routingKey: " + returnedMessage.getRoutingKey());
    }
}
